package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.DoingLoveDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingLove;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DoingLoveDaoImpl extends SqlSessionDaoSupport implements DoingLoveDao {
    private final static Logger logger = LoggerFactory.getLogger(DoingLoveDaoImpl.class);

    @Override
    public void insert(DoingLove doingLove) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.insertDoingLove", doingLove);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Insert database failure.", e);
            }
            throw new AppBusinessException("Write database failure.", e);
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<DoingLove> getList(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<DoingLove>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getList database failure.", e);
            }
            throw new AppBusinessException("getList database failure.", e);
        }
    }

    @Override
    public int getListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getListCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getListCount failure.", e);
            }
            throw new AppBusinessException("getListCount failure.", e);
        }
    }

    @Override
    public void update(DoingLove doingLove) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.updateDoingLove", doingLove);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Update database failure.", e);
            }
            throw new AppBusinessException("Update database failure.", e);
        }
    }

    @Override
    public void deleteLoveDoing(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.deleteLoveDoing", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Delete database failure.", e);
            }
            throw new AppBusinessException("Delete database failure.", e);
        }

    }

    @Override
    public DoingLove getDoingLove(Map<String, Object> map) throws AppBusinessException {
        try {
            return (DoingLove) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getDoingLove", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Doing> loveMeList(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Doing>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.loveMeList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Doing> myLoveDoingList(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Doing>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.myLoveDoingList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @Override
    public int loveMeListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.loveMeListCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("loveMeListCount failure.", e);
            }
            throw new AppBusinessException("loveMeListCount failure.", e);
        }
    }

    @Override
    public int myLoveDoingListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.myLoveDoingListCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("myLoveDoingListCount failure.", e);
            }
            throw new AppBusinessException("myLoveDoingListCount failure.", e);
        }
    }

    @Override
    public int getMyFamilyLoveAmount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getMyFamilyLoveAmount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    @Override
    public int getMyLoveDoingCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getMyLoveDoingCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("query database failure.", e);
            }
            throw new AppBusinessException("query database failure.", e);
        }
    }

    @Override
    public int getMemberLoveCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingLoveMapper.getMemberLoveCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getDoingLoveCount failure.", e);
            }
            throw new AppBusinessException("getDoingLoveCount failure.", e);
        }
    }

}
